<?php
/**
 * Unit Converter helper class
 * 
 * @version
 *
 */
class Reale_Controller_Helper_UnitConverter extends Zend_Controller_Action_Helper_Abstract
{
    static $_factor = array('sqft' => 1, 'sqmeter' => .0930000000000, 
    'sqyard' => .1111111111111, 'acre' => .0000229566300, 
    'hectare' => .000009290304, 'marla' => .0001852119400);
    /**
     * Enter description here ...
     * @param int|float $value
     * @param string $fromUnit
     * @param string $toUnit
     * @return int|float $result
     */
    static function direct ($value, $fromUnit, $toUnit = 'sqft')
    {
        $value = (float)$value;
        
        if ((is_real($value) or is_int($value)) and $value) {
        } else {
            throw new Exception('Value is not acceptable :' . var_export($value, true),
                                                                            Zend_Log::ERR);
        }
        if (!is_string($fromUnit) and !array_key_exists($fromUnit, self::$_factor)) {
            throw new Exception('Source unit is not acceptable :' . var_export($fromUnit, true), 
                                                                            Zend_Log::ERR);
        }
        if (!is_string($toUnit) and !array_key_exists($toUnit, self::$_factor)) {
            throw new Exception(
            'Target unit is not acceptable :' . var_export($toUnit, true), 
                                                                Zend_Log::ERR);
        }
        $result = ($value * self::$_factor[$toUnit]) / self::$_factor[$fromUnit];
        $result = (string)round($result,4);
        return $result;
    }
}